<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆id" prop="carId">
        <el-input
          v-model="queryParams.carId"
          placeholder="请输入车辆id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="carNo">
        <el-input
          v-model="queryParams.carNo"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="devicename">
        <el-input
          v-model="queryParams.devicename"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="devicetype">
        <el-select v-model="queryParams.devicetype" placeholder="请选择设备类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="sim号码" prop="simnum">
        <el-input
          v-model="queryParams.simnum"
          placeholder="请输入sim号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="overduetime">
        <el-input
          v-model="queryParams.overduetime"
          placeholder="请输入过期时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到期通知时间" prop="expirenotifytime">
        <el-input
          v-model="queryParams.expirenotifytime"
          placeholder="请输入到期通知时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="哪个用户创建的，方便快速查找分组" prop="creater">
        <el-input
          v-model="queryParams.creater"
          placeholder="请输入哪个用户创建的，方便快速查找分组"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="默认是多少个摄像头" prop="videochannelcount">
        <el-input
          v-model="queryParams.videochannelcount"
          placeholder="请输入默认是多少个摄像头"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上一次在线时间" prop="lastactivetime">
        <el-input
          v-model="queryParams.lastactivetime"
          placeholder="请输入上一次在线时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否能正常使用 1正常使用 2 正在体验 3 已禁用4 设备到期 5 设备过期" prop="isfree">
        <el-input
          v-model="queryParams.isfree"
          placeholder="请输入是否能正常使用 1正常使用 2 正在体验 3 已禁用4 设备到期 5 设备过期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="0代表不允许二级 普通用户编辑 1代表允许" prop="allowedit">
        <el-input
          v-model="queryParams.allowedit"
          placeholder="请输入0代表不允许二级 普通用户编辑 1代表允许"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="默认是小车，图标" prop="icon">
        <el-input
          v-model="queryParams.icon"
          placeholder="请输入默认是小车，图标"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否关注 0 代表未关注 1代表关注" prop="stared">
        <el-input
          v-model="queryParams.stared"
          placeholder="请输入是否关注 0 代表未关注 1代表关注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录名称" prop="loginname">
        <el-input
          v-model="queryParams.loginname"
          placeholder="请输入登录名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后查询位置时间" prop="lastquerypositiontime">
        <el-input
          v-model="queryParams.lastquerypositiontime"
          placeholder="请输入最后查询位置时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用 0未启用，1启用" prop="isAvailable">
        <el-input
          v-model="queryParams.isAvailable"
          placeholder="请输入是否启用 0未启用，1启用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户id" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gwebmgr:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gwebmgr:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gwebmgr:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['gwebmgr:device:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备序列号，全局唯一" align="center" prop="deviceid" />
      <el-table-column label="车辆id" align="center" prop="carId" />
      <el-table-column label="车牌号" align="center" prop="carNo" />
      <el-table-column label="设备名称" align="center" prop="devicename" />
      <el-table-column label="设备类型" align="center" prop="devicetype" />
      <el-table-column label="sim号码" align="center" prop="simnum" />
      <el-table-column label="过期时间" align="center" prop="overduetime" />
      <el-table-column label="到期通知时间" align="center" prop="expirenotifytime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="哪个用户创建的，方便快速查找分组" align="center" prop="creater" />
      <el-table-column label="默认是多少个摄像头" align="center" prop="videochannelcount" />
      <el-table-column label="上一次在线时间" align="center" prop="lastactivetime" />
      <el-table-column label="是否能正常使用 1正常使用 2 正在体验 3 已禁用4 设备到期 5 设备过期" align="center" prop="isfree" />
      <el-table-column label="0代表不允许二级 普通用户编辑 1代表允许" align="center" prop="allowedit" />
      <el-table-column label="默认是小车，图标" align="center" prop="icon" />
      <el-table-column label="是否关注 0 代表未关注 1代表关注" align="center" prop="stared" />
      <el-table-column label="登录名称" align="center" prop="loginname" />
      <el-table-column label="最后查询位置时间" align="center" prop="lastquerypositiontime" />
      <el-table-column label="是否启用 0未启用，1启用" align="center" prop="isAvailable" />
      <el-table-column label="租户id" align="center" prop="tenantId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gwebmgr:device:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gwebmgr:device:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备51GPS对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车辆id" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入车辆id" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="form.carNo" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="devicename">
          <el-input v-model="form.devicename" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备类型" prop="devicetype">
          <el-select v-model="form.devicetype" placeholder="请选择设备类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="sim号码" prop="simnum">
          <el-input v-model="form.simnum" placeholder="请输入sim号码" />
        </el-form-item>
        <el-form-item label="过期时间" prop="overduetime">
          <el-input v-model="form.overduetime" placeholder="请输入过期时间" />
        </el-form-item>
        <el-form-item label="到期通知时间" prop="expirenotifytime">
          <el-input v-model="form.expirenotifytime" placeholder="请输入到期通知时间" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="哪个用户创建的，方便快速查找分组" prop="creater">
          <el-input v-model="form.creater" placeholder="请输入哪个用户创建的，方便快速查找分组" />
        </el-form-item>
        <el-form-item label="默认是多少个摄像头" prop="videochannelcount">
          <el-input v-model="form.videochannelcount" placeholder="请输入默认是多少个摄像头" />
        </el-form-item>
        <el-form-item label="上一次在线时间" prop="lastactivetime">
          <el-input v-model="form.lastactivetime" placeholder="请输入上一次在线时间" />
        </el-form-item>
        <el-form-item label="是否能正常使用 1正常使用 2 正在体验 3 已禁用4 设备到期 5 设备过期" prop="isfree">
          <el-input v-model="form.isfree" placeholder="请输入是否能正常使用 1正常使用 2 正在体验 3 已禁用4 设备到期 5 设备过期" />
        </el-form-item>
        <el-form-item label="0代表不允许二级 普通用户编辑 1代表允许" prop="allowedit">
          <el-input v-model="form.allowedit" placeholder="请输入0代表不允许二级 普通用户编辑 1代表允许" />
        </el-form-item>
        <el-form-item label="默认是小车，图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入默认是小车，图标" />
        </el-form-item>
        <el-form-item label="是否关注 0 代表未关注 1代表关注" prop="stared">
          <el-input v-model="form.stared" placeholder="请输入是否关注 0 代表未关注 1代表关注" />
        </el-form-item>
        <el-form-item label="登录名称" prop="loginname">
          <el-input v-model="form.loginname" placeholder="请输入登录名称" />
        </el-form-item>
        <el-form-item label="最后查询位置时间" prop="lastquerypositiontime">
          <el-input v-model="form.lastquerypositiontime" placeholder="请输入最后查询位置时间" />
        </el-form-item>
        <el-form-item label="删除状态" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除状态" />
        </el-form-item>
        <el-form-item label="是否启用 0未启用，1启用" prop="isAvailable">
          <el-input v-model="form.isAvailable" placeholder="请输入是否启用 0未启用，1启用" />
        </el-form-item>
        <el-form-item label="租户id" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice, exportDevice } from "@/api/gwebmgr/device";

export default {
  name: "Device",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备51GPS表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carId: null,
        carNo: null,
        devicename: null,
        devicetype: null,
        simnum: null,
        overduetime: null,
        expirenotifytime: null,
        creater: null,
        videochannelcount: null,
        lastactivetime: null,
        isfree: null,
        allowedit: null,
        icon: null,
        stared: null,
        loginname: null,
        lastquerypositiontime: null,
        isAvailable: null,
        tenantId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备51GPS列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceid: null,
        carId: null,
        carNo: null,
        devicename: null,
        devicetype: null,
        simnum: null,
        overduetime: null,
        expirenotifytime: null,
        remark: null,
        creater: null,
        videochannelcount: null,
        lastactivetime: null,
        isfree: null,
        allowedit: null,
        icon: null,
        stared: null,
        loginname: null,
        lastquerypositiontime: null,
        delFlag: null,
        isAvailable: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备51GPS";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceid = row.deviceid || this.ids
      getDevice(deviceid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备51GPS";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceid != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceids = row.deviceid || this.ids;
      this.$modal.confirm('是否确认删除设备51GPS编号为"' + deviceids + '"的数据项？').then(function() {
        return delDevice(deviceids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有设备51GPS数据项？').then(() => {
        this.exportLoading = true;
        return exportDevice(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
